package cn.com.main;

import cn.com.consts.TemplateConst;
import cn.com.utils.FileUtil;
import cn.hutool.core.util.StrUtil;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 模板生成内容
 *
 * @author LiuGuodong
 * @date 2024/02/21
 */
public class TemplateGeneratedContent {

    //表与inset的映射
    public static final Map<String, String> STRING_MAP =
            Map.of(TemplateConst.INSERT, TemplateConst.STRING_START,
                    TemplateConst.ACTION, TemplateConst.ACTION_START,
                    TemplateConst.MODULE_ACTION, TemplateConst.MODULE_ACTION_START);
    //表与格式化方法的映射
    public static final Map<String, Function<String[], String>> FUNCTION_MAP =
            Map.of(TemplateConst.INSERT, TemplateGeneratedContent::builderFormat,
                    TemplateConst.ACTION, TemplateGeneratedContent::actionFormat,
                    TemplateConst.MODULE_ACTION, TemplateGeneratedContent::moduleActionFormat);
    //表名
    public static final List<String> TABLE_NAME = List.of(TemplateConst.INSERT, TemplateConst.ACTION, TemplateConst.MODULE_ACTION);

    /**
     * 路径
     */
    public static final String PATH = "E:\\cloud_star\\240108浦发银行项目\\应用与数据集成平台 ROMA Connect 半集成\\";

    /**
     * 资源编码
     */
    private static final String RESOURCE_ENCODING = "WAF";
    /**
     * 菜单编码
     */
    private static final String MENU_ENCODING = "DG02";



    public static void main(String[] args) throws IOException {
        String first = PATH + "input.txt";
        final List<String> value = FileUtil.getReadAllLines(first);
        StringBuilder countBuilder = new StringBuilder();
        //iam 权限
        iamAuthority(value, countBuilder);

        addLineBreaks(countBuilder);
        // 系统权限
        countBuilder.append(TemplateConst.RES_AUTHORITY
                .replaceAll(TemplateConst.RESOURCE_ENCODING, RESOURCE_ENCODING)
                .replaceAll(TemplateConst.MENU_ENCODING, MENU_ENCODING));

        FileUtil.writeToFile(countBuilder, PATH + RESOURCE_ENCODING + "all.sql");

    }

    private static void addLineBreaks(StringBuilder countBuilder) {
        countBuilder.append("\n\n");
    }

    private static void iamAuthority(List<String> value, StringBuilder countBuilder) {
        TABLE_NAME.forEach(tableName -> {
            StringBuilder actionBuilder = new StringBuilder();
            generateSqlQueries(value, actionBuilder, tableName);
            countBuilder.append(actionBuilder);
            addLineBreaks(countBuilder);
            FileUtil.writeToFile(actionBuilder, PATH + tableName + ".sql");
        });
    }

    private static void generateSqlQueries(List<String> value, StringBuilder actionBuilder, String type) {
        actionBuilder.append(STRING_MAP.get(type));
        value.stream().distinct().filter(StrUtil::isAllNotBlank).forEach(line -> {
            String[] split = line.split("\t");
            actionBuilder.append(FUNCTION_MAP.get(type).apply(split)).append(",\n");
        });
        deleteCharAt(actionBuilder);
    }

    private static void deleteCharAt(StringBuilder actionBuilder) {
        actionBuilder.deleteCharAt(actionBuilder.length() - 2).append(";");
    }

    public static String builderFormat(String[] split) {
        return String.format(TemplateConst.INSERT_TEMPLATE, split[2], split[3], split[1], split[0]);
    }

    public static String moduleActionFormat(String[] split) {
        return String.format(TemplateConst.MODULE_ACTION_TEMPLATE, MENU_ENCODING, split[2]);
    }

    public static String actionFormat(String[] split) {
        return String.format(TemplateConst.ACTION_TEMPLATE, split[2], split[3], split[1], RESOURCE_ENCODING);
    }


}
